//
//  MySQLConnection.Tests.m
//  LibMySQL
//
//  Created by Marcus Grenängen on 2009-07-17.
//  Copyright 2009 SneWs Soft. All rights reserved.
//
#import "MySQLConnection.Tests.h"


@implementation MySQLConnection_Tests

- (void) setUp {
	dbManager = [[MySQLConnection alloc] initWithHost: @"127.0.0.1" port: 3306 user: @"root" password: @"root"];
	STAssertNotNil( dbManager, @"dbManager is NULL!" );
}

- (void) tearDown {
	if( dbManager != nil )
		[dbManager release];
}


#pragma mark -
#pragma mark Test methods starts from here



/// Test methods starts from here

- (void)testEscapeString {
	NSString *testString = @"That's what I thought!";
	NSString *expected = @"That\\'s what I thought!";
	
	NSString *actual = [dbManager escapeString: testString];
	STAssertTrue( [actual compare: expected] == NSOrderedSame, @"Result string does not match the expected string..." );
	
	[actual release];
	[testString release];
	[expected release];
}

- (void)testPrepareValue {
	NSString *testString = @"StringValue";
	NSString *expected = @"'StringValue'";
	
	NSString *actual = [dbManager prepareValue: testString];
	STAssertTrue( [actual compare: expected] == NSOrderedSame, @"Result string does not match the expected string...(String value)" );
	
	[actual release];
	[testString release];
	[expected release];
	
	NSNull *nullValue = [NSNull null];
	expected = @"NULL";
	
	actual = [dbManager prepareValue: nullValue];
	STAssertTrue( [actual compare: expected] == NSOrderedSame, @"Result string does not match the expected string...(NULL value)" );
	
	[actual release];
	[nullValue release];
}


- (void) testGetServerVersion {
	@try {
		NSString* serverVersion = [dbManager serverVersion];
		NSLog( @"Server version is %@", serverVersion );
		
		[serverVersion release];
	}
	@catch( MySQLException* e ) {
		NSLog( @"Error performing call to serverVersion(): %@", e );
		[e release];
	}
	@finally {
	}
	
} // END METHOD: testGetServerVersion: 


- (void) testNewConnectionWithConnectionString {
	MySQLConnection* c = [MySQLConnection connectionWithString: @"Server=127.0.0.1;Port=3306;Uid=root;Pwd=root"];
	
	@try {
		[c open];
		
		NSString* serverVersion = (NSString*)[c executeScalar: @"SELECT VERSION()"];
		NSLog( @"Server at %@ and port %li is running with version: %@", [c hostname], [c port], serverVersion );
		[serverVersion release];
		
		[c close];
	}
	@catch ( MySQLException* e ) {
		NSLog( @"Error: %@", e );
		[e release];
	}
	@finally {
		if( c != nil ) {
			[c release];
		}
	}
}


- (void) testChangeDatabase {
	@try {
		[dbManager changeDatabase: @"INFORMATION_SCHEMA"];
	}
	@catch( MySQLException* e ) {
		NSLog( @"Error changing database: %@", e );
		[e release];
	}
	@finally {
	}
}

- (void) testGetSingleValueWithProperType {
	@try {
		id val = [[dbManager executeScalar: @"SELECT COUNT( * ) FROM user;"] retain];
		NSLog( @"%@ users in MySQL's user table.", val );
		
		[val release];
	}
	@catch( MySQLException* e ) {
		NSLog( @"Error performing call to executeScalar(): %@", e );
		[e release];
	}
	@finally {
	}
}

- (void) testGetLastInsertedID {
	@try {
		NSInteger val = [dbManager lastInsertedId];
		NSLog( @"Last inserted id = %li", val );
	}
	@catch( MySQLException* e ) {
		NSLog( @"Error performing call to lastInsertedId(): %@", e );
		[e release];
	}
	@finally {
	}
}

- (void) testCharsetHandling {
	@try {
		[dbManager open];
		NSLog( @"Current charset is %@", [dbManager charset] );
		
		[dbManager changeCharacterSet: @"utf8"];
	}
	@catch( MySQLException* e ) {
		NSLog( @"Error performing call to changeCharacterSet(): %@", e );
		[e release];
	}
	@finally {
	}
}




- (void) testFetchFullResult {
	MySQLResult* result = nil;
	@try {
		[dbManager changeCharacterSet: @"utf8"];
		result = [dbManager executeQuery: @"SELECT u0.*, u.* FROM `user` u LEFT OUTER JOIN user_info u0 ON u0.User = u.User ORDER BY u0.Full_name ASC;"];
		NSLog( @"# of columns %li and row count is %li", [result columnCount], [result rowCount] );
		
		NSArray* allColumns = [result columns];
		for( MySQLColumn* col in allColumns )
			NSLog( @"Column with name: %@", [col name] );
		
		[allColumns release];
		
		
		for( MySQLRow* row in result ) {
			NSLog( @"%@ - %@ for host %@ - Has super privileges? %@", 				  
				  ( [row getValueByName: @"Full_name"] != [NSNull null] ? [row getValueByName: @"Full_name"] : @"(No name)" ),
				  [row getValueByName: @"User"], 
				  [row getValueByName: @"Host"],
				  [row getValueByName: @"Super_priv"] );
		
		} // END for( MySQLRow* row in result )

		
	} // END @try
	@catch( MySQLException* e ) {
		NSLog( @"Error: %@", e );
		[e release];
	
	} // END @catch( MySQLException* e )
	@catch( NSException* e ) {
		NSLog( @"Error, (NSError*): %@", e );
		[e release];
	
	} // END @catch( NSException* e )
	@finally {
		if( result != nil ) {
			[result release];
		}
	
	} // END @finally

} // END TEST METHOD: testFetchFullResult: 


- (void) testFetchImageFromHelperMethod {
	/// TODO: Re-enable when the binary data handling is completed.
	/*
	MySQLResult* result = nil;
	@try {
		result = [dbManager executeQuery: @"SELECT u0.User, u0.Icon FROM `user` u LEFT OUTER JOIN user_info u0 ON u0.User = u.User WHERE u0.Icon <> 'NULL' ORDER BY u0.Full_name ASC;"];
		NSLog( @"# of columns %li and row count is %li", [result columnCount], [result rowCount] );
		
		for( MySQLRow* row in result ) {
			NSImage* img = [row getImageByName: @"Icon"];
			//NSAssert( img != nil, @"Unable to extract MySQL bin data as NSImage*" );
			STAssertNotNil( img, @"Unable to extract Image from MySQL result set." );
			
			if( img != nil ) {
				NSLog( @"Success for Image (W:%li x H:%li) fetch with user: %@", 
					  [img size].width,
					  [img size].height,
					  [row getValueByName: @"User"] );
				
				[img release];
			
			} // END if( img != nil )
		
		} // END for( MySQLRow* row in result )

	} // END @try
	@catch( MySQLException* me ) {
		NSLog( @"Error: %@", me );
		[me release];

	}
	@catch( NSException* e ) {
		NSLog( @"Error, (NSError*): %@", e );
		[e release];
		
	}
	@finally {
		if( result != nil ) {
			[result release];
		}
		
	} // END @finally
	 */
}



@end










